/**
 * @author rxliuli
 */
const {
    DateFormatter: {
        dateTimeFormatter,
    },
} = rx
const app = new BasicTableVue({
    el: '#app',
    data() {
        return {
            api: moFilingSuperviseApi,
            columns: [
                new TableColumn({width: 30, type: 'selection'}),
                new TableColumn({
                    prop: 'houseId',
                    title: '所在机房',
                    formatter: ({houseId}) => {
                        return this.houseMap.get(houseId)
                    },
                }),
                new TableColumn({prop: 'ip', title: '应用IP'}),
                new TableColumn({prop: 'topDomain', title: '顶级域名'}),
                new TableColumn({prop: 'subDomainCount', title: '子域名数', customComponent: true}),
                new TableColumn({
                    prop: 'filingStatus',
                    title: '备案状态',
                    formatter: fieldFormatter.facade(fieldFormatter.moFilingSupervise.filingStatusMap),
                }),
                new TableColumn({prop: 'filingNumber', title: '备案号'}),
                new TableColumn({
                    prop: 'dealStatus',
                    title: '处置',
                    formatter: fieldFormatter.facade(fieldFormatter.moFilingSupervise.dealStatusMap),
                }),
                new TableColumn({
                    prop: 'dealResult',
                    title: '阻断',
                    formatter: fieldFormatter.facade(fieldFormatter.moFilingSupervise.dealResultMap),
                }),
                new TableColumn({prop: 'firstGatherTime', title: '首次采集时间', formatter: TableColumn.datetimeFormat}),
                new TableColumn({prop: 'lastActiveTime', title: '最后活跃时间', formatter: TableColumn.datetimeFormat}),
            ],
            houses: [],
            moFilingSuperviseMap: new Map(),
            dealPanelShow: false,
            dealPanelForm: {
                dealStatus: 4,
            },
            domains: [],
            dialogTableVisible: false,
            title: '',
            subDomains: [],
        }
    },
    methods: {
        createForm() {
            return new MoFilingSupervise({
                topDomainAction: 1,
                subDomainAction: 1,
                ipAction: 1,
                filingNumberAction: 1,
                houseId: '',
                dealStatus: '',
                filingStatus: '',
            })
        },
        toggleDealPanelShow() {
            this.dealPanelShow = !this.dealPanelShow
        },
        async getPage(page, entity) {
            const {firstGatherTimeRange, lastActiveTimeRange, ...args} = entity
            const result = await this.api.page(page, args)
            this.moFilingSuperviseMap = rx.arrayToMap(result.records, ({id}) => id)
            return result
        },
        getContent(dealStatus, v) {
            switch (dealStatus) {
                case 1:
                case 2:
                    return v.domain
                default:
                    return null
            }
        },
        /**
         * 处置
         */
        async dealSelected() {
            const disposedIds = this.selectedList.filter((obj) => parseInt(obj.dealStatus) !== 1).map((obj) => obj.id)
            if (disposedIds.length !== 0) {
                rxPrompt.msg('只能勾选未处置的数据');
                return;
            }
            const ids = this.selectedList.filter((obj) => parseInt(obj.dealStatus) === 1).map((obj) => obj.id)
            const {
                dealStatus,
                remark,
            } = this.dealPanelForm
            if (this.checkSelected() && await ajaxOption({
                res: this.api.dealBatch({
                    v1: dealStatus,
                    v2: remark,
                    v3: ids.map(id => {
                        const v = this.moFilingSuperviseMap.get(id)
                        const content = this.getContent(dealStatus, v)
                        return {...v, content}
                    }),
                }),
                danger: '处置失败',
                success: '处置成功',
            })) {
                await this.searchPage()
            }
        },
        /**
         * 取消处置
         */
        async dealCancelSelected() {
            const disposedIds = this.selectedList.filter((obj) => parseInt(obj.dealStatus) !== 2).map((obj) => obj.id)
            if (disposedIds.length !== 0) {
                rxPrompt.msg('只能勾选已处置的数据');
                return;
            }
            const ids = this.selectedList.filter((obj) => parseInt(obj.dealStatus) === 2).map((obj) => obj.id)
            if (this.checkSelected() && await ajaxOption({
                res: this.api.dealCancelBatch(ids.map(id => {
                    const v = this.moFilingSuperviseMap.get(id)
                    const content = this.getContent(v.dealStatus, v)
                    return {...v, content}
                })),
                danger: '取消处置错误',
                success: '取消处置成功',
            })) {
                await this.searchPage()
            }
        },
        async filingSelected() {
            if (this.checkSelected() && await ajaxOption({
                res: this.api.filingBatch(this.selectedIdList.map(id => this.moFilingSuperviseMap.get(id))),
                danger: '备案检查错误',
                success: '备案检查成功',
            })) {
                await this.searchPage()
            }
        },
        async init() {
            this.houses = await baseHouseInfoApi.selectList();
        },
        initDealPanel() {
            this.dealPanelForm = {
                dealStatus: 4,
                logOperation: undefined,
                remark: undefined,
            }
        },
        showWindow(data) {
            this.title = '域名列表'
            this.domains = data.subDomains
            this.domains.forEach(domain => {
                domain.findDomainListFirstTime = dateTimeFormatter.format(new Date(domain.findDomainListFirstTime))
                domain.findDomainListLastTime = dateTimeFormatter.format(new Date(domain.findDomainListLastTime))
            })
            this.dialogTableVisible = true
        },

    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },
})